#property copyright   "Copyright 2013,mingxinhe@gmail.com"
#property link "mingxinhe@gmail.com"

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4 
#property  indicator_width1  2

//---- indicator parameters
extern string Envelopes_Setting ="------Custom Envelopes Setting------";
extern string Envelopes_IndicatorName = "Envelopes";
extern int    MA_Period=14;
extern int    MA_Shift=0;
extern string MA_Method.Desc = "0-SMA,1-EMA,2-SMMA,3-LWMA,4-T3MA,5-JMA,6-JFATL,7-J2JMA,8-ParMA";
extern int    MA_Method=0;
extern string Applied_Price.Desc = "0-CLOSE,1-OPEN,2-HIGH,3-LOW,4-MEDIAN,5-TYPICAL,6-WEIGHTED,7-SIMPLE,8-QUARTER,9-TRENDFOLLOW,10-0.5*TRENDFOLLOW";
extern int    Applied_Price=0;
extern double T3_b_=70;                      //Coefficient x100  
extern int    JMAPhase=100; //Smoothing parameter
extern int    MA_period2 = 5; // Depth of the second smoothing                    
extern double JMAPhase2=100;  // Second smoothing parameter,
extern double Deviation1=0.1;
extern double Deviation2=0.1;
extern string MAIndicatorName = "Custom Moving Average";
extern color  A = Blue;
extern color  B = Red;
extern color  C = Gray;
extern int    MA_Period_4Difference=10;
//---- indicator buffers
double     EnvelopesDifBufferA[], EnvelopesDifBufferB[], EnvelopesDifBufferC[], EnvelopesDifBuffer[];
double     MABuffer[];

double max = -1, min = 1000000;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   IndicatorBuffers(5);
   
   SetIndexBuffer(0,EnvelopesDifBufferA);
   SetIndexBuffer(1,EnvelopesDifBufferB);
   SetIndexBuffer(2,EnvelopesDifBufferC);   
   SetIndexBuffer(3,MABuffer);
   SetIndexBuffer(4,EnvelopesDifBuffer);
   SetIndexStyle(0,DRAW_HISTOGRAM,EMPTY,EMPTY,A);
   SetIndexStyle(1,DRAW_HISTOGRAM,EMPTY,EMPTY,B);
   SetIndexStyle(2,DRAW_HISTOGRAM,EMPTY,EMPTY,C);
   SetIndexStyle(3,DRAW_LINE,EMPTY,EMPTY,Yellow);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Envelopes_Dif("+MA_Period+","+ Deviation1 +","+ MA_Shift +")");
   SetIndexLabel(0,"DifferenceA");
   SetIndexLabel(1,"DifferenceB");
   SetIndexLabel(2,"DifferenceC");
   SetIndexLabel(3,"Average");
   
   
//---- initialization done
   return(0);
  }
  
void deinit() 
{
    ObjectDelete("max");
    ObjectDelete("min");
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   limit=Bars-ExtCountedBars;
      
 //---- macd counted in the 1-st buffer
    for(int i= limit-1; i >= 0; i--)
    {
        //double upperBand = iEnvelopes(NULL, 0, ma_period, ma_method, ma_shift, applied_price, deviation,MODE_UPPER,i);                
        double upperBand = iCustom(NULL, 0, Envelopes_IndicatorName, MA_Period, MA_Shift, MA_Method, Applied_Price, T3_b_, JMAPhase, MA_period2, 
                           JMAPhase2, Deviation1, Deviation2, MAIndicatorName, 0, 1);  
        double lowerBand = iCustom(NULL, 0, Envelopes_IndicatorName, MA_Period, MA_Shift, MA_Method, Applied_Price, T3_b_, JMAPhase, MA_period2, 
                           JMAPhase2, Deviation1, Deviation2, MAIndicatorName, 1, 1);                              
        if (Close[i] > upperBand)
        {
            EnvelopesDifBuffer[i] = High[i] - upperBand;
            EnvelopesDifBufferA[i] = EnvelopesDifBuffer[i];
            if (max < EnvelopesDifBuffer[i])
            {
                max = EnvelopesDifBuffer[i];
            }
            if (min > EnvelopesDifBuffer[i])
            {
                min = EnvelopesDifBuffer[i];
            }
        }
        else if (Close[i] < lowerBand)
        {                
            //double lowerBand = iEnvelopes(NULL, 0, ma_period, ma_method, ma_shift, applied_price, deviation,MODE_LOWER,i);                
            EnvelopesDifBuffer[i] = lowerBand - Low[i];
            EnvelopesDifBufferB[i] = EnvelopesDifBuffer[i];
            if (max < EnvelopesDifBuffer[i])
            {
                max = EnvelopesDifBuffer[i];
            }
            if (min > EnvelopesDifBuffer[i])
            {
                min = EnvelopesDifBuffer[i];
            }
        } 
        else
        {
            EnvelopesDifBuffer[i] = MathAbs((High[i] + Low[i])/2.0 - (upperBand + lowerBand)/2.0);
            EnvelopesDifBufferC[i] = EnvelopesDifBuffer[i];
            if (max < EnvelopesDifBuffer[i])
            {
                max = EnvelopesDifBuffer[i];
            }
            if (min > EnvelopesDifBuffer[i])
            {
                min = EnvelopesDifBuffer[i];
            }
        }
        
        /*
        int barCount = Bars - 1;
        if (i == barCount)
        {
            MABuffer[i] = EnvelopesDifBuffer[i];
            //Print(i, " ", MABuffer[i]);
        }
        else
        {
            MABuffer[i] = (MABuffer[i + 1]*(barCount - i) + EnvelopesDifBuffer[i])/(barCount - i + 1);
            //Print(i, " ", MABuffer[i], " ", MABuffer[i + 1], " ", barCount, " ",EnvelopesDifBuffer[i]);
        }
        */
    }
    
    for (i= limit-1; i >= 0; i--)
    {
        MABuffer[i] = iMAOnArray(EnvelopesDifBuffer, 0, MA_Period_4Difference, 0, MODE_SMA, i);
    }
        
     //---- signal line counted in the 2-nd buffer
        
         if (ObjectFind("max") == -1)
         {
             int wnd = WindowFind("Envelopes_Dif("+MA_Period+","+ Deviation1 +","+ MA_Shift +")");
             ObjectCreate("max", OBJ_LABEL, wnd, 0, 0);
             ObjectSet("max", OBJPROP_CORNER, 1);
             ObjectSet("max", OBJPROP_XDISTANCE, 10);
             ObjectSet("max", OBJPROP_YDISTANCE, 30);
             ObjectSetText("max", "Max:" + max, 10, "Times New Roman", Yellow);
         } 
         else
         {
             ObjectSetText("max", "Max:" + max, 10, "Times New Roman", Yellow);
         }
         
         if (ObjectFind("min") == -1)
         {
             wnd = WindowFind("Envelopes_Dif("+MA_Period+","+ Deviation1 +","+ MA_Shift +")");
             ObjectCreate("min", OBJ_LABEL, wnd, 0, 0);
             ObjectSet("min", OBJPROP_CORNER, 1);
             ObjectSet("min", OBJPROP_XDISTANCE, 10);
             ObjectSet("min", OBJPROP_YDISTANCE, 50);
             ObjectSetText("min", "Min:" + min, 10, "Times New Roman", Yellow);
         } 
         else
         {
             ObjectSetText("min", "Min:" + min, 10, "Times New Roman", Yellow);
         } 
         
//---- done

    
   return(0);
 }
 
//+------------------------------------------------------------------+